package com.qire.manhua.dal.service;

import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.basic.StringUtil;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.common.model.entity.UserEntity;
import com.qire.common.support.base.ProxyViewModel;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.R;
import com.qire.manhua.dal.IUserDataDAL;
import com.qire.manhua.model.viewModel.IAccountsAndSecurityViewModel;
import com.qire.manhua.model.vo.OkResponseVO;
import com.qire.manhua.model.vo.UserDetailVO;
import com.qire.other.thirdPlatform.authorize.ThirdPlatformUserInfo;

public class AccountsAndSecurityService extends DataService {

    private final IUserDataDAL userDataDAL = WarehouseFactory.create(this, IUserDataDAL.class);
    private final ProxyViewModel<IAccountsAndSecurityViewModel> viewModel;

    public AccountsAndSecurityService(ProxyViewModel<IAccountsAndSecurityViewModel> viewModel) {
        this.viewModel = viewModel;
        refreshViewModel();
    }

    public void qqBind(ThirdPlatformUserInfo userInfo) {
        userDataDAL.qqBind(userInfo.openid, userInfo.nickname);
    }
    public void qqUnBind(ThirdPlatformUserInfo userInfo) {
        userDataDAL.qqUnbind(userInfo.openid);
    }
    public void weChatBind(ThirdPlatformUserInfo userInfo) {
        userDataDAL.weChatBind(userInfo.unionid, userInfo.openid, userInfo.nickname);
    }
    public void weChatUnBind(ThirdPlatformUserInfo userInfo) {
        userDataDAL.weChatUnbind(userInfo.unionid);
    }
    @StrategyAnnotation(event = {Url.WeChatBind, Url.QqBind, Url.WeChatUnbind, Url.QqUnbind})
    public void thirdPartyOperateCallback(OkResponseVO responseOkVO, boolean isCache) {
        ToastHelper.showSnackBar(responseOkVO.ok, Prompt.SUCCESS);
        userDataDAL.refreshUserDetailData();
    }
    @StrategyAnnotation(event = Url.myIndex)
    public void refreshUserDetailDataCallback(UserDetailVO dataVO, boolean isCache) {
        if(ObjectUtil.nonNull(dataVO) && !isCache) {
            dataVO.updateUserEntity();
            refreshViewModel();
        }
    }

    private void refreshViewModel() {
        if(ObjectUtil.nonNull(viewModel)) {
            refreshMobileInfo();
            refreshThirdPlatformInfo();
        }
    }

    private void refreshMobileInfo() {
        String mobile = UserEntity.self.getMobile();
        boolean haveMobileBind = StringUtil.notEmpty(mobile);
        viewModel.set("mobileBindHintText", haveMobileBind ? StringUtil.replace("****", mobile, 3) : "");
        viewModel.set("changePasswordHintText", haveMobileBind ? "" : "请先绑定手机号");
    }

    private void refreshThirdPlatformInfo() {
        String weChatName = UserEntity.self.getWeChatName();
        String qqName = UserEntity.self.getQqName();
        boolean haveWeChatBind = StringUtil.notEmpty(weChatName);
        boolean haveQqBind = StringUtil.notEmpty(qqName);

        String thirdPlatformHintText = "";
        if(haveWeChatBind) {
            thirdPlatformHintText = StringUtil.replace("***", weChatName, 1, weChatName.length());
        }
        if(haveQqBind) {
            thirdPlatformHintText = StringUtil.replace("***", weChatName, 1, weChatName.length());
        }

        viewModel.set("thirdPlatformHintText", thirdPlatformHintText);

        viewModel.set("weChatBindHintText", haveWeChatBind ? String.format("(%s)", weChatName) : "");
        viewModel.set("weChatBindBtnText", haveWeChatBind ? "解除绑定" : "绑定");
        viewModel.set("weChatBindBtnTextColor", haveWeChatBind ? "#ff5050" : "#ffffff");
        viewModel.set("weChatBindBtnBackground", haveWeChatBind ? 0 : R.drawable.app_account_security_button_green);

        viewModel.set("qqBindHintText", haveQqBind ? String.format("(%s)", qqName) : "");
        viewModel.set("qqBindBtnText", haveQqBind ? "解除绑定" : "绑定");
        viewModel.set("qqBindBtnTextColor", haveQqBind ? "#ff5050" : "#ffffff");
        viewModel.set("qqBindBtnBackground", haveQqBind ? 0 : R.drawable.app_account_security_button_green);
    }

}
